﻿/*
* Project: HOERP
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2011年5月14日 星期六 上午 10:36:39
*/

package com.serp.act.xt;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;
import org.tulip.DaoException;

import com.serp.Constants;
import com.serp.action.BaseAct;
import com.serp.business.xt.Custom;
import com.serp.business.xt.CustomDao;
import com.serp.form.xt.CustomForm;

import com.serp.web.ec.ECConstants;

/**
 * 客户Action
 */
public class CustomAct extends BaseAct {
	
	@Override
	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		CustomForm customForm = (CustomForm) form;
		if (null != customForm) {
		 				
			// 新增
			if (Constants.ACT_ADD.equals(customForm.getAct())) {
				if (isTokenValid(request)) {
					addCustom(customForm);
					setFormNull(customForm);
				} else {
					setFormNull(customForm);
					customForm.setMessages("请不要重复提交!");
				}
			}

			// 删除
			else if (Constants.ACT_DEL.equals(customForm.getAct())) {
				delCustom(customForm);
				setFormNull(customForm);
			}
			// 编辑
			else if (Constants.ACT_EDIT.equals(customForm.getAct())) {
				editCustom(customForm);
				customForm.setAct(Constants.ACT_UPDATE);
			}
			// 编辑后保存--更新
			else if (Constants.ACT_UPDATE.equals(customForm.getAct())) {
				if (isTokenValid(request)) {
					updateCustom(customForm);
					//setFormNull(customForm);
				} else {
					setFormNull(customForm);
					customForm.setMessages("请不要重复提交!");
				}

			}
			//默认显示
			else {
				setFormNull(customForm);
			}

		}

		// 显示列表
		displayTable(request);
		
		ActionForward returnActionForward = mapping.findForward("success");

		logger.debug("execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end"); 
		return returnActionForward;
	}
	


	/**
	 * 显示客户列表
	 */
	private void displayTable(HttpServletRequest request) throws DaoException {
		CustomDao dao = new CustomDao();
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		Limit limit = new TableLimit(limitFactory);
		int totalRows = dao.getTotalCustoms();
		limit.setRowAttributes(totalRows,
				ECConstants.PAGE_DEFAULT_ROWS_DISPALAY);

		Sort sort = limit.getSort();
		String prop = sort.getAlias();
		String sortOrder = sort.getSortOrder();
		List customs = dao.getCustoms(prop, sortOrder, limit.getCurrentRowsDisplayed(), limit.getRowStart());
		request.setAttribute("totalRows", new Integer(totalRows));
		request.setAttribute("Customs", customs);
	}

    /**
	 * 更新客户
	 */
	private void updateCustom(CustomForm customForm) {
		Custom custom = new Custom();
		try {
			BeanUtils.copyProperties(custom, customForm);
			new CustomDao().updateCustom(custom);
		} catch (Exception e) {
			customForm.setMessages("更新客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 编辑客户
	 */
	private void editCustom(CustomForm customForm) {
		try {
			Custom obj = new CustomDao().getCustomByID(customForm.getCustId());
			
	        customForm.setCustId(obj.getCustId());
	        customForm.setCustName(obj.getCustName());
	        customForm.setCustTypeId(obj.getCustTypeId());
	        customForm.setCustAddr(obj.getCustAddr());
	        customForm.setCustPhone(obj.getCustPhone());
	        customForm.setCustTel(obj.getCustTel());
	        customForm.setCustContName(obj.getCustContName());
	        customForm.setCustStat(obj.getCustStat());
	        customForm.setCustArea(obj.getCustArea());
	        customForm.setCustFax(obj.getCustFax());
	        customForm.setCustMailBox(obj.getCustMailBox());
	        customForm.setCustEmail(obj.getCustEmail());
	        customForm.setCustCoun(obj.getCustCoun());
	        customForm.setCustContJob(obj.getCustContJob());
	        customForm.setCustCompDesc(obj.getCustCompDesc());
	        customForm.setCustBank(obj.getCustBank());
	        customForm.setCustBankAcco(obj.getCustBankAcco());
	        customForm.setCustSwDjh(obj.getCustSwDjh());
	        customForm.setCustSfCredMana(obj.getCustSfCredMana());
	        customForm.setCustSaleMode(obj.getCustSaleMode());
	        customForm.setCustIncrRate(obj.getCustIncrRate());
	        customForm.setCustCity(obj.getCustCity());
	        customForm.setCustProv(obj.getCustProv());
	        customForm.setCustHomePage(obj.getCustHomePage());
	        customForm.setCurrId(obj.getCurrId());
	        customForm.setCustCurrName(obj.getCustCurrName());
	        customForm.setCustPaymType(obj.getCustPaymType());
	        customForm.setCustCorpRepr(obj.getCustCorpRepr());
	        customForm.setCustCustCode(obj.getCustCustCode());
	        customForm.setCustDeseExpeTaxaCode(obj.getCustDeseExpeTaxaCode());
	        customForm.setCustAdvaAccoCode(obj.getCustAdvaAccoCode());
	        customForm.setCustPayExpeTaxaCode(obj.getCustPayExpeTaxaCode());
	        customForm.setCustPrefPoli(obj.getCustPrefPoli());
	        customForm.setCustBranPassDept(obj.getCustBranPassDept());
	        customForm.setCustSpecSale(obj.getCustSpecSale());
	        customForm.setCustLastTradDate(obj.getCustLastTradDate());
	        customForm.setCustLastTradMone(obj.getCustLastTradMone());
	        customForm.setCustLastDeseSkDate(obj.getCustLastDeseSkDate());
	        customForm.setCustLastDeseMone(obj.getCustLastDeseMone());
	        customForm.setCustMaxTradMone(obj.getCustMaxTradMone());
	        customForm.setCustMinDeseRate(obj.getCustMinDeseRate());
	        customForm.setCustMinSubsRate(obj.getCustMinSubsRate());
	        customForm.setCustCzLevel(obj.getCustCzLevel());
	        customForm.setCustAccoTotal(obj.getCustAccoTotal());
	        customForm.setCustPaymTotal(obj.getCustPaymTotal());
	        customForm.setCustForgTotal(obj.getCustForgTotal());
	        customForm.setCustCred(obj.getCustCred());
	        customForm.setCustSpecKefu(obj.getCustSpecKefu());
	        customForm.setCustSfCwInit(obj.getCustSfCwInit());
						
		} catch (DaoException e) {
			customForm.setMessages("查询客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 初始化（置空）客户
	 */
	private void setFormNull(CustomForm customForm) {
		//customForm.setAct(Constants.ACT_ADD);
		
	    customForm.setCustId(0);
	    customForm.setCustName(null);
	    customForm.setCustTypeId(0);
	    customForm.setCustAddr(null);
	    customForm.setCustPhone(null);
	    customForm.setCustTel(null);
	    customForm.setCustContName(null);
	    customForm.setCustStat(0);
	    customForm.setCustArea(null);
	    customForm.setCustFax(null);
	    customForm.setCustMailBox(null);
	    customForm.setCustEmail(null);
	    customForm.setCustCoun(null);
	    customForm.setCustContJob(null);
	    customForm.setCustCompDesc(null);
	    customForm.setCustBank(null);
	    customForm.setCustBankAcco(null);
	    customForm.setCustSwDjh(null);
	    customForm.setCustSfCredMana(0);
	    customForm.setCustSaleMode(null);
	    customForm.setCustIncrRate(0.0);
	    customForm.setCustCity(null);
	    customForm.setCustProv(null);
	    customForm.setCustHomePage(null);
	    customForm.setCurrId(0);
	    customForm.setCustCurrName(null);
	    customForm.setCustPaymType(0);
	    customForm.setCustCorpRepr(null);
	    customForm.setCustCustCode(null);
	    customForm.setCustDeseExpeTaxaCode(null);
	    customForm.setCustAdvaAccoCode(null);
	    customForm.setCustPayExpeTaxaCode(null);
	    customForm.setCustPrefPoli(null);
	    customForm.setCustBranPassDept(0);
	    customForm.setCustSpecSale(0);
	    customForm.setCustLastTradDate(0);
	    customForm.setCustLastTradMone(0.0);
	    customForm.setCustLastDeseSkDate(0);
	    customForm.setCustLastDeseMone(0.0);
	    customForm.setCustMaxTradMone(0.0);
	    customForm.setCustMinDeseRate(0.0);
	    customForm.setCustMinSubsRate(0.0);
	    customForm.setCustCzLevel(0);
	    customForm.setCustAccoTotal(0.0);
	    customForm.setCustPaymTotal(0.0);
	    customForm.setCustForgTotal(0.0);
	    customForm.setCustCred(0.0);
	    customForm.setCustSpecKefu(0);
	    customForm.setCustSfCwInit(0);
		
		customForm.setMessages(null);
	}

	/**
	 * 删除客户
	 */
	private void delCustom(CustomForm customForm) {
		Custom custom = new Custom();
		try {
			BeanUtils.copyProperties(custom, customForm);
			new CustomDao().delCustom(custom);
		} catch (Exception e) {
			customForm.setMessages("删除客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 添加客户
	 */
	private void addCustom(CustomForm customForm){
		Custom custom = new Custom();
		try {
			BeanUtils.copyProperties(custom, customForm);
			new CustomDao().addCustom(custom);
		} catch (Exception e) {
			customForm.setMessages("添加客户失败!" + e.getMessage());
			e.printStackTrace();
		}
	}


}